package com.tebiecloud.base.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.base.client.constants.BaseConstants;
import com.tebiecloud.base.client.model.UserAccount;
import com.tebiecloud.base.client.model.entity.BaseAccount;
import com.tebiecloud.base.client.model.entity.BaseApp;
import com.tebiecloud.base.client.model.entity.BaseUser;
import com.tebiecloud.base.client.service.IBaseAppServiceClient;
import com.tebiecloud.base.client.service.IBaseUserServiceClient;
import com.tebiecloud.base.server.service.BaseAccountService;
import com.tebiecloud.base.server.service.BaseAppService;
import com.tebiecloud.base.server.service.BaseUserService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.security.SecurityClientDetails;
import com.tebiecloud.common.utils.RandomValueUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.WebUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户管理
 * @Author: tebie
 * @Date: 2019-08-09 11:05
 */
@RestController
@RequestMapping("/feign/user")
public class UserController implements IBaseUserServiceClient {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private BaseAccountService baseAccountService;

    /**
     * 获取登录账号信息
     * @param username 登录名
     * @return
     */
    @PostMapping("infouseraccountforlogin")
    @Override
    public ResultBody<UserAccount> getUserAccountForLogin(@RequestParam(value = "username") String username,
                                                          @RequestParam(value = "password") String password,
                                                          @RequestParam(value = "type") String type) {
        UserAccount userAccount = baseUserService.getUserAccountForLogin(username,password,type);
        if(userAccount == null) {
            return ResultBodyUtils.failed();
        }

        return ResultBodyUtils.ok(userAccount);
    }

    /**
     * 获取登录账号信息
     * @param account 账号
     * @param accountType 登录类型
     * @param domain 登录域
     * @return
     */
    @PostMapping("infoBaseAccount")
    @Override
    public ResultBody<BaseAccount> getBaseAccount(@RequestParam(value = "account") String account,
                                                  @RequestParam(value = "accountType",required = false) String accountType,
                                                  @RequestParam(value = "domain",required = false) String domain) {
        BaseAccount baseAccount = baseAccountService.getAccount(account, accountType, domain);
        if(baseAccount == null) {
            return ResultBodyUtils.failed();
        }

        return ResultBodyUtils.ok(baseAccount);
    }

    /**
     * 添加登录账号
     * @param account
     * @param password
     * @param accountType
     * @param domain
     * @param userDesc
     * @param nickName
     * @param avatar
     * @return
     */
    @PostMapping("addBaseAccount")
    @Override
    public ResultBody<BaseAccount> addBaseAccount(
            @RequestParam(value = "account") String account,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "accountType") String accountType,
            @RequestParam(value = "domain") String domain,
            @RequestParam(value = "userDesc") String userDesc,
            @RequestParam(value = "nickName", required = false) String nickName,
            @RequestParam(value = "avatar", required = false) String avatar
    ) {
        // 添加用户信息
        BaseUser baseUser = new BaseUser();
        baseUser.setUserName(account);
        baseUser.setNickName(nickName);
        baseUser.setAvatar(avatar);
        baseUser.setUserType(BaseConstants.USER_TYPE_USER);
        baseUser.setUserDesc(userDesc);
        baseUser.setStatus(1); // 1正常
        baseUserService.save(baseUser);

        // 添加登录账号
        BaseAccount baseAccount = new BaseAccount();
        baseAccount.setAccount(account);
        baseAccount.setAccountType(accountType);
        baseAccount.setDomain(domain);
        baseAccount.setPassword(passwordEncoder.encode(password));
        baseAccount.setRegisterIp(WebUtils.getRemoteAddress(WebUtils.getHttpServletRequest()));
        baseAccount.setStatus(1);
        baseAccount.setUserId(baseUser.getUserId());
        baseAccountService.save(baseAccount);

        return ResultBodyUtils.ok(baseAccount);
    }

    /**
     * 更新账号状态
     * @param account
     * @param status
     * @return
     */
    @PostMapping("updateBaseAccount")
    @Override
    public ResultBody<BaseAccount> updateBaseAccount(String account, int status) {
        UpdateWrapper<BaseAccount> baseAccountUpdateWrapper = new UpdateWrapper<>();
        baseAccountUpdateWrapper.eq("account",account);
        BaseAccount baseAccount = new BaseAccount();
        baseAccount.setStatus(status);
        baseAccountService.update(baseAccount,baseAccountUpdateWrapper);
        return ResultBodyUtils.ok(baseAccount);
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @PostMapping("infoBaseUser")
    @Override
    public ResultBody<BaseUser> infoBaseUser(@RequestParam(value = "userId") long userId) {
        QueryWrapper<BaseUser> baseUserQueryWrapper = new QueryWrapper<>();
        baseUserQueryWrapper.eq("user_id",userId);
        return ResultBodyUtils.ok(baseUserService.getOne(baseUserQueryWrapper));
    }

    /**
     * 获取账户信息
     * @param accountId
     * @return
     */
    @PostMapping("getBaseAccountById")
    @Override
    public ResultBody<BaseAccount> getBaseAccountById(@RequestParam(value = "accountId") long accountId) {
        BaseAccount baseAccount = baseAccountService.getOne(new QueryWrapper<BaseAccount>().eq("account_id",accountId));
        if(baseAccount == null) {
            return ResultBodyUtils.failed();
        }
        return ResultBodyUtils.ok(baseAccount);
    }

    @PostMapping("getBaseAccountByUserId")
    @Override
    public ResultBody<BaseAccount> getBaseAccountByUserId(long userId) {
        BaseAccount baseAccount = baseAccountService.getOne(new QueryWrapper<BaseAccount>().eq("user_id",userId));
        if(baseAccount == null) {
            return ResultBodyUtils.failed();
        }
        return ResultBodyUtils.ok(baseAccount);
    }
}
